<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <title>用户历史订单</title>
</head>

<body>
    <h2>用户历史订单</h2>
    <form action="" method="get">
        查询订单:
        <input type="radio" name="method" value="0" <?php if (!isset($_GET['method']) || $_GET['method'] == 0) echo 'checked="true"'; ?>">订单号
        <input type="radio" name="method" value="1" <?php if (!(!isset($_GET['method']) || $_GET['method'] == 0)) echo 'checked="true"'; ?>">菜品名
        <input type="text" name="key">
        <input type="submit" value="查询">
    </form>
    <div>
        <hr>
        <?php
        require_once('db_fns.php');
        $conn = db_connect();
        if (session_status() == PHP_SESSION_NONE) {
            session_start();
        }
        $uid = $_SESSION['uid'];
        $array = array();
        $isset_key = isset($_GET['key']);
        if ($isset_key) {
            if ($_GET['method'] == 1) {
                $key = $_GET['key'];
                $sql = "call sp_query_order_id_by_uid_key({$uid},'%{$key}%')";
                $result = $conn->query($sql);
                while ($row = $result->fetch_array()) {
                    array_push($array, $row['oid']);
                }
                $conn = db_connect();
            } else {
                array_push($array, $_GET['key']);
            }
        }
        $sql = "call sp_query_user_historical_orders_by_uid({$uid})";
        $result = $conn->query($sql);
        while ($row = $result->fetch_array()) {
            if ($isset_key && !in_array($row['oid'], $array)) {
                continue;
            }
        ?>
            <p>订单号：<?php echo $row['oid']; ?></p>
            <p>订单时间：<?php echo $row['time']; ?></p>
            <p>订单状态：<?php echo $row['state']; ?></p>
            <p>订单总价：￥<?php echo $row['total']; ?></p>
            <p>订单详情：</p>
            <table border="1" cellspacing="1">
                <tr>
                    <th width="200px" style="background:#66ccff;">名称</th>
                    <th width="100px" style="background:#66ccff;">单价</th>
                    <th width="100px" style="background:#66ccff;">数量</th>
                    <th width="200px" style="background:#66ccff;">商家名</th>
                </tr>
                <?php
                require_once('db_fns.php');
                $conn = db_connect();
                $sql2 = "call sp_query_order_details_by_oid({$row['oid']})";
                $detail = $conn->query($sql2);
                while ($row2 = $detail->fetch_array()) {
                ?>
                    <tr>
                        <td><?php echo $row2['dname']; ?></td>
                        <td>￥<?php echo $row2['unit-price']; ?></td>
                        <td style="text-align: center;"><?php echo $row2['quantity']; ?></td>
                        <td><?php echo $row2['mname']; ?></td>
                    </tr>
                <?php
                }
                ?>
            </table>
            <hr>
        <?php
        }
        ?>
    </div>
    <a href=user_center.php>返回首页</a>
</body>

</html>